﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace HITs
{
    public partial class Chart_Group : Form
    {
        List<List<string>> listGroup;
        List<List<string>> listPhanNganh;
        List<string> listMaNganh;
        int soNgayHienThi = 150;

        public Chart_Group()
        {
            InitializeComponent();
        }

        private void Chart_Group_Load(object sender, EventArgs e)
        {
            listGroup = new List<List<string>>();
            listPhanNganh = new List<List<string>>();

            #region Group
            listGroup.Add(new List<string>());
            List<string> content = CoreFunctions.File_LoadAnyFile(System.Windows.Forms.Application.StartupPath + @"\HOSE\Chart_Group.txt");
            foreach (string line in content)
            {
                string[] part = line.Split(new string[] { "\t", " " }, StringSplitOptions.RemoveEmptyEntries);
                if (line.Contains("-"))
                {
                    listGroup.Add(new List<string>());
                    continue;
                }
                else
                    foreach (string nameCP in part)
                        if (ShareData.danhsachCoPhieu.DanhSachTenCoPhieu.Contains(nameCP))
                            listGroup[listGroup.Count - 1].Add(nameCP);
            }

            for (int i = 0; i < listGroup.Count; i++)
                if (listGroup[i].Count == 0)
                {
                    listGroup.RemoveAt(i);
                    i--;
                }

            cmbx_Group01.Items.Clear();
            cmbx_Group02.Items.Clear();
            for (int i = 0; i < listGroup.Count; i++)
            {
                cmbx_Group02.Items.Add("Group" + (i+1).ToString());
                cmbx_Group01.Items.Add("Group" + (i+1).ToString());
            }
            #endregion

            #region Ngành
            List<string> fileload = CoreFunctions.File_LoadAnyFile(System.Windows.Forms.Application.StartupPath 
                + @"\Data\Nganh_DanhSachPhanNganh.txt");
            listPhanNganh = new List<List<string>>();
            foreach (string list in fileload)
            {
                List<string> listCP = new List<string>();
                string[] part = list.Split(new string[] { "\t", " " }, StringSplitOptions.RemoveEmptyEntries);
                if (part.Length > 0)
                {
                    listCP.AddRange(part);
                    listPhanNganh.Add(listCP);
                }
            }

            List<string> listTenCp = ShareData.danhsachCoPhieu.DanhSachTenCoPhieu;
            for(int i = 0 ; i < listPhanNganh.Count;i++)
                for(int j = 0 ; j < listPhanNganh[i].Count;j++)
                    if (listTenCp.IndexOf(listPhanNganh[i][j]) < 0)
                    {
                        listPhanNganh[i].RemoveAt(j);
                        j--;
                    }

            fileload = CoreFunctions.File_LoadAnyFile(System.Windows.Forms.Application.StartupPath + @"\Data\Nganh_MaNganh_TenNganh.txt");
            listMaNganh = new List<string>();
            foreach (string list in fileload)
            {
                string[] part = list.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries);
                listMaNganh.Add(part[0]);
            }
            cmbx_Nganh.Items.Clear();
            foreach (string item in listMaNganh)
                cmbx_Nganh.Items.Add(item);
            #endregion

            if (listGroup.Count != 0)
            {
                cmbx_Group01.SelectedIndex = 0;
            }
        }

        private void cmbx_Group01_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbx_Group01.Items.Count != 0)
            {
                List<string> groupCP = listGroup[cmbx_Group01.SelectedIndex];
                cmbx_Group02.SelectedIndex = cmbx_Group01.SelectedIndex;

                chart_Group01.Series.Clear();
                chart_Group01.ChartAreas[0].AxisX.Enabled = AxisEnabled.False;
                chart_Group01.ChartAreas[0].AxisY2.Enabled = AxisEnabled.False;
                chart_Group01.ChartAreas[0].AxisY.LineColor = Color.Silver;
                chart_Group01.ChartAreas[0].AxisY.LineDashStyle = ChartDashStyle.Dot;
                chart_Group01.ChartAreas[0].AxisY2.LineColor = Color.Silver;
                chart_Group01.ChartAreas[0].AxisY2.LineDashStyle = ChartDashStyle.Dot;

                List<float> listZscore = new List<float>();
                for (int i = 0; i < soNgayHienThi; i++)
                    listZscore.Add(0f);
                foreach (string cp in groupCP)
                {
                    int index = ShareData.danhsachCoPhieu.DanhSachTenCoPhieu.IndexOf(cp);
                    List<float> listGia = CoreFunctions
                        .Xuly_LayNphantudautien(ShareData.danhsachCoPhieu.DanhSachCoPhieu_List[index].DanhSachGiaDongCua, soNgayHienThi);
                    for (int i = 0; i < soNgayHienThi; i++)
                        listZscore[i] += CoreFunctions.Tk_GetZScores(listGia)[i];
                }
                for (int i = 0; i < soNgayHienThi; i++)
                    listZscore[i] = (float)Math.Round(listZscore[i] / groupCP.Count, 1);

                List<float> MACD = new List<float>();
                List<float> signalLine = new List<float>();
                Indicators.MACD(listZscore, 12, 26, 9, ref MACD, ref signalLine);

                List<float> BB_tren = new List<float>();
                List<float> BB_duoi = new List<float>();
                List<float> BB_giua = new List<float>();
                Indicators.BollingerBand(listZscore, 20, ref BB_giua, ref BB_tren, ref BB_duoi);

                List<float> listChechLech = new List<float>();
                for (int i = 0; i < MACD.Count && i < signalLine.Count; i++)
                    listChechLech.Add(MACD[i] - signalLine[i]);

                int count_step = 0;
                float temp_value = listChechLech[0];
                List<float> list_TKChuky = new List<float>();
                for (int i = 0; i < listChechLech.Count; i++)
                {
                    if ((temp_value < 0f && listChechLech[i] < 0)
                        || (temp_value > 0f && listChechLech[i] > 0))
                        count_step++;
                    else
                    {
                        list_TKChuky.Add(count_step);
                        count_step = 0;
                        temp_value = listChechLech[i];
                    }
                }

                float max, min, average;
                CoreFunctions.Tk_ThongKeMoTa(list_TKChuky, out max, out min, out average);
                lb_TKChuky.Text = "TK sóng - max = " + max + " - min = " + min + " - avg = " + Math.Round(average, 0);
                lb_TKChuky.Text += " - std = " + Math.Round(CoreFunctions.Tk_SampleStandardDeviation(list_TKChuky), 0);

               
                #region Vẽ Chart
                chart_Group01.Series.Add("Zscore");
                chart_Group01.Series[chart_Group01.Series.Count - 1].ChartType = SeriesChartType.Spline;
                chart_Group01.Series[chart_Group01.Series.Count - 1].BorderWidth = 3;   
                chart_Group01.Series[chart_Group01.Series.Count - 1]
                    .Points.DataBindY(listZscore);

                chart_Group01.Series.Add("MACD");
                chart_Group01.Series[chart_Group01.Series.Count - 1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
                chart_Group01.Series[chart_Group01.Series.Count - 1].BorderWidth = 2;
                chart_Group01.Series[chart_Group01.Series.Count - 1].Color = Color.GreenYellow;
                chart_Group01.Series[chart_Group01.Series.Count - 1].YAxisType = AxisType.Secondary;
                chart_Group01.Series[chart_Group01.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(MACD, soNgayHienThi));

                chart_Group01.Series.Add("Signal Line");
                chart_Group01.Series[chart_Group01.Series.Count - 1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
                chart_Group01.Series[chart_Group01.Series.Count - 1].BorderWidth = 2;
                chart_Group01.Series[chart_Group01.Series.Count - 1].Color = Color.GreenYellow;
                chart_Group01.Series[chart_Group01.Series.Count - 1].BorderDashStyle = ChartDashStyle.DashDotDot;
                chart_Group01.Series[chart_Group01.Series.Count - 1].YAxisType = AxisType.Secondary;
                chart_Group01.Series[chart_Group01.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(signalLine, soNgayHienThi));

                chart_Group01.Series.Add("BB Trên");
                chart_Group01.Series[chart_Group01.Series.Count - 1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
                chart_Group01.Series[chart_Group01.Series.Count - 1].BorderWidth = 1;
                chart_Group01.Series[chart_Group01.Series.Count - 1].Color = Color.DimGray;
                chart_Group01.Series[chart_Group01.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(BB_tren, soNgayHienThi));

                chart_Group01.Series.Add("BB Dưới");
                chart_Group01.Series[chart_Group01.Series.Count - 1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
                chart_Group01.Series[chart_Group01.Series.Count - 1].BorderWidth = 1;
                chart_Group01.Series[chart_Group01.Series.Count - 1].Color = Color.DimGray;
                chart_Group01.Series[chart_Group01.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(BB_duoi, soNgayHienThi));

                chart_MACD.Series.Clear();
                chart_MACD.ChartAreas[0].AxisX.Enabled = AxisEnabled.False;

                chart_MACD.Series.Add("Histogram MACD");
                chart_MACD.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
                chart_MACD.Series[0].Points.DataBindY(listChechLech);
                #endregion
            }
        }

        private void cmbx_group02_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbx_Group01.SelectedIndex = cmbx_Group02.SelectedIndex;

            chart_Group02.Series.Clear();
            chart_Group02.ChartAreas[0].AxisX.Enabled = AxisEnabled.False;
            chart_Group02.ChartAreas[0].AxisY2.Enabled = AxisEnabled.False;
            chart_Group02.ChartAreas[0].AxisY.LineColor = Color.Silver;
            chart_Group02.ChartAreas[0].AxisY.LineDashStyle = ChartDashStyle.Dot;
            chart_Group02.ChartAreas[0].AxisY2.LineColor = Color.Silver;
            chart_Group02.ChartAreas[0].AxisY2.LineDashStyle = ChartDashStyle.Dot;

            List<string> groupCP = listGroup[cmbx_Group02.SelectedIndex];
            List<float> listZscore = new List<float>();
            for (int i = 0; i < soNgayHienThi; i++)
                listZscore.Add(0f);

            foreach (string cp in groupCP)
            {
                int index = ShareData.danhsachCoPhieu.DanhSachTenCoPhieu.IndexOf(cp);
                List<float> listGia = CoreFunctions
                    .Xuly_LayNphantudautien(ShareData.danhsachCoPhieu.DanhSachCoPhieu_List[index].DanhSachGiaDongCua, soNgayHienThi);
                chart_Group02.Series.Add(cp);
                chart_Group02.Series[chart_Group02.Series.Count - 1].ChartType = SeriesChartType.Spline;
                chart_Group02.Series[chart_Group02.Series.Count - 1].BorderWidth = 3;
                chart_Group02.Series[chart_Group02.Series.Count - 1].Points.DataBindY(CoreFunctions.Tk_GetZScores(listGia));
            }
        }

        private void cmbx_Nganh_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbx_Nganh.Items.Count != 0)
            {
                List<string> groupCP = listPhanNganh[cmbx_Nganh.SelectedIndex];
                
                chart_Group01.Series.Clear();
                chart_Group01.ChartAreas[0].AxisX.Enabled = AxisEnabled.False;
                chart_Group01.ChartAreas[0].AxisY2.Enabled = AxisEnabled.False;
                chart_Group01.ChartAreas[0].AxisY.LineColor = Color.Silver;
                chart_Group01.ChartAreas[0].AxisY.LineDashStyle = ChartDashStyle.Dot;
                chart_Group01.ChartAreas[0].AxisY2.LineColor = Color.Silver;
                chart_Group01.ChartAreas[0].AxisY2.LineDashStyle = ChartDashStyle.Dot;

                List<float> listZscore = new List<float>();
                for (int i = 0; i < soNgayHienThi; i++)
                    listZscore.Add(0f);
                foreach (string cp in groupCP)
                {
                    int index = ShareData.danhsachCoPhieu.DanhSachTenCoPhieu.IndexOf(cp);
                    List<float> listGia = CoreFunctions
                        .Xuly_LayNphantudautien(ShareData.danhsachCoPhieu.DanhSachCoPhieu_List[index].DanhSachGiaDongCua, soNgayHienThi);
                    for (int i = 0; i < soNgayHienThi; i++)
                        listZscore[i] += CoreFunctions.Tk_GetZScores(listGia)[i];
                }
                for (int i = 0; i < soNgayHienThi; i++)
                    listZscore[i] = (float)Math.Round(listZscore[i] / groupCP.Count, 1);

                List<float> MACD = new List<float>();
                List<float> signalLine = new List<float>();
                Indicators.MACD(listZscore, 12, 26, 9, ref MACD, ref signalLine);

                List<float> BB_tren = new List<float>();
                List<float> BB_duoi = new List<float>();
                List<float> BB_giua = new List<float>();
                Indicators.BollingerBand(listZscore, 20, ref BB_giua, ref BB_tren, ref BB_duoi);

                List<float> listChechLech = new List<float>();
                for (int i = 0; i < MACD.Count && i < signalLine.Count; i++)
                    listChechLech.Add(MACD[i] - signalLine[i]);

                int count_step = 0;
                float temp_value = listChechLech[0];
                List<float> list_TKChuky = new List<float>();
                for (int i = 0; i < listChechLech.Count; i++)
                {
                    if ((temp_value < 0f && listChechLech[i] < 0)
                        || (temp_value > 0f && listChechLech[i] > 0))
                        count_step++;
                    else
                    {
                        list_TKChuky.Add(count_step);
                        count_step = 0;
                        temp_value = listChechLech[i];
                    }
                }

                float max, min, average;
                CoreFunctions.Tk_ThongKeMoTa(list_TKChuky, out max, out min, out average);
                lb_TKChuky.Text = "TK sóng - max = " + max + " - min = " + min + " - avg = " + Math.Round(average, 0);
                lb_TKChuky.Text += " - std = " + Math.Round(CoreFunctions.Tk_SampleStandardDeviation(list_TKChuky), 0);


                #region Vẽ Chart 1
                chart_Group01.Series.Add("Zscore");
                chart_Group01.Series[chart_Group01.Series.Count - 1].ChartType = SeriesChartType.Spline;
                chart_Group01.Series[chart_Group01.Series.Count - 1].BorderWidth = 3;
                chart_Group01.Series[chart_Group01.Series.Count - 1]
                    .Points.DataBindY(listZscore);

                chart_Group01.Series.Add("MACD");
                chart_Group01.Series[chart_Group01.Series.Count - 1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
                chart_Group01.Series[chart_Group01.Series.Count - 1].BorderWidth = 2;
                chart_Group01.Series[chart_Group01.Series.Count - 1].Color = Color.GreenYellow;
                chart_Group01.Series[chart_Group01.Series.Count - 1].YAxisType = AxisType.Secondary;
                chart_Group01.Series[chart_Group01.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(MACD, soNgayHienThi));

                chart_Group01.Series.Add("Signal Line");
                chart_Group01.Series[chart_Group01.Series.Count - 1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
                chart_Group01.Series[chart_Group01.Series.Count - 1].BorderWidth = 2;
                chart_Group01.Series[chart_Group01.Series.Count - 1].Color = Color.GreenYellow;
                chart_Group01.Series[chart_Group01.Series.Count - 1].BorderDashStyle = ChartDashStyle.DashDotDot;
                chart_Group01.Series[chart_Group01.Series.Count - 1].YAxisType = AxisType.Secondary;
                chart_Group01.Series[chart_Group01.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(signalLine, soNgayHienThi));

                chart_Group01.Series.Add("BB Trên");
                chart_Group01.Series[chart_Group01.Series.Count - 1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
                chart_Group01.Series[chart_Group01.Series.Count - 1].BorderWidth = 1;
                chart_Group01.Series[chart_Group01.Series.Count - 1].Color = Color.DimGray;
                chart_Group01.Series[chart_Group01.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(BB_tren, soNgayHienThi));

                chart_Group01.Series.Add("BB Dưới");
                chart_Group01.Series[chart_Group01.Series.Count - 1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
                chart_Group01.Series[chart_Group01.Series.Count - 1].BorderWidth = 1;
                chart_Group01.Series[chart_Group01.Series.Count - 1].Color = Color.DimGray;
                chart_Group01.Series[chart_Group01.Series.Count - 1].Points.DataBindY(CoreFunctions.Xuly_LayNphantudautien(BB_duoi, soNgayHienThi));

                chart_MACD.Series.Clear();
                chart_MACD.ChartAreas[0].AxisX.Enabled = AxisEnabled.False;

                chart_MACD.Series.Add("Histogram MACD");
                chart_MACD.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
                chart_MACD.Series[0].Points.DataBindY(listChechLech);
                #endregion

                #region Vẽ Chart 2
                chart_Group02.Series.Clear();
                chart_Group02.ChartAreas[0].AxisX.Enabled = AxisEnabled.False;
                chart_Group02.ChartAreas[0].AxisY2.Enabled = AxisEnabled.False;
                chart_Group02.ChartAreas[0].AxisY.LineColor = Color.Silver;
                chart_Group02.ChartAreas[0].AxisY.LineDashStyle = ChartDashStyle.Dot;
                chart_Group02.ChartAreas[0].AxisY2.LineColor = Color.Silver;
                chart_Group02.ChartAreas[0].AxisY2.LineDashStyle = ChartDashStyle.Dot;

                listZscore = new List<float>();
                for (int i = 0; i < soNgayHienThi; i++)
                    listZscore.Add(0f);

                foreach (string cp in groupCP)
                {
                    int index = ShareData.danhsachCoPhieu.DanhSachTenCoPhieu.IndexOf(cp);
                    List<float> listGia = CoreFunctions
                        .Xuly_LayNphantudautien(ShareData.danhsachCoPhieu.DanhSachCoPhieu_List[index].DanhSachGiaDongCua, soNgayHienThi);
                    chart_Group02.Series.Add(cp);
                    chart_Group02.Series[chart_Group02.Series.Count - 1].ChartType = SeriesChartType.Spline;
                    chart_Group02.Series[chart_Group02.Series.Count - 1].BorderWidth = 3;
                    chart_Group02.Series[chart_Group02.Series.Count - 1].Points.DataBindY(CoreFunctions.Tk_GetZScores(listGia));
                }
                #endregion
            }
        }
    }
}
